#include "router.h"
#include "ipmacscan.h"
#include "get_interface.h"
#include "menu.h"

#include <pthread.h>
#include <fcntl.h>
#include <unistd.h> 
#include <ctype.h>

netCardIfo cardIfo[16];
int netNum;

// 判断客户端是否发送数据给路由器
int ipMacRouterCmp(uc8* dstIp, uc8* dstMac)
{
    if(dstIp == NULL && dstMac == NULL){
        printf("IP和MAC都没有输入\n");
        DEBUGERROR;
        return -1;
    }

    int ret = -1;
    if(dstIp != NULL && dstMac != NULL){
        for(int i = 0; i < netNum; ++i){
            ret = strcmp(cardIfo[i].cardIp, dstIp);
            if(ret == 0){
                break;
            }else{
                ret = -1;
            }
        }
    }else if(dstIp != NULL){
        for(int i = 0; i < netNum; ++i){
            ret = strcmp(cardIfo[i].cardIp, dstIp);
            if(ret == 0){
                break;
            }else{
                ret = -1;
            }
        }
    }else if(dstMac != NULL){
        for(int i = 0; i < netNum; ++i){
            ret = strcmp(cardIfo[i].cardMac, dstMac);
            if(ret == 0){
                break;
            }else{
                ret = -1;
            }
        }
    }
    return ret;
}

// arp扫描函数,网卡对应局域网下的ip和mac扫描和保存
int arpScanFunc(ipMacIfo* ifoIpMacFd)
{
    
    ipMac* scanIpMacHead = createIpMacHead();
    ipMac* filterIpMacHead = createIpMacHead();
    ifoIpMacFd->ipMacHead = scanIpMacHead;
    ifoIpMacFd->ipMacFilterHead = filterIpMacHead;

    // 获取网卡信息
    getinterface();
   
    // ens33和ens38网卡配置
    char* ensName[20]; // 用于存储网卡名称的字符串数组
    uc8 srcip[ARRMAX];
    memset(srcip, 0, sizeof(srcip));
    uc8 srcmac[ARRMAX] = ""; 
    memset(srcmac, 0, sizeof(srcmac));

    ifoIpMacFd->ensCnt = 0;

    for(int i = 0; i < get_interface_num(); i++){
        int ret = strncmp(net_interface[i].name, "ens", 3);
        if(ret == 0){
            memset(&(cardIfo[ifoIpMacFd->ensCnt]), 0, sizeof(cardIfo[ifoIpMacFd->ensCnt]));
            sprintf(cardIfo[ifoIpMacFd->ensCnt].cardIp, "%d.%d.%d.%d",
                    net_interface[i].ip[0], net_interface[i].ip[1], 
                    net_interface[i].ip[2], net_interface[i].ip[3]);
            sprintf(cardIfo[ifoIpMacFd->ensCnt].cardMac, "%02x:%02x:%02x:%02x:%02x:%02x",
                    net_interface[i].mac[0], net_interface[i].mac[1], net_interface[i].mac[2],
                    net_interface[i].mac[3], net_interface[i].mac[4], net_interface[i].mac[5]);

            int ipStartAddress = ipAddress(ifoIpMacFd->ensCnt); // ip访问起始地址获取
            int macStartAddress = macAddress(ifoIpMacFd->ensCnt); // ip访问起始地址获取
            ifoIpMacFd->netCard[ifoIpMacFd->ensCnt] = net_interface[i].name;
            srcip[ipStartAddress] = net_interface[i].ip[0]; 
            srcip[ipStartAddress+1] = net_interface[i].ip[1];
            srcip[ipStartAddress+2] = net_interface[i].ip[2]; 
            srcip[ipStartAddress+3] = net_interface[i].ip[3];

            srcmac[macStartAddress] = net_interface[i].mac[0]; 
            srcmac[macStartAddress+1] = net_interface[i].mac[1]; 
            srcmac[macStartAddress+2] = net_interface[i].mac[2];
            srcmac[macStartAddress+3] = net_interface[i].mac[3]; 
            srcmac[macStartAddress+4] = net_interface[i].mac[4]; 
            srcmac[macStartAddress+5] = net_interface[i].mac[5];

            ifoIpMacFd->ensCnt++;
        }
        continue;
    }

    ifoIpMacFd->sourceIp = srcip;
    ifoIpMacFd->sourceMac = srcmac;
    netNum = ifoIpMacFd->ensCnt;
    arpReply(&ifoIpMacFd);
    // displayIpMacList(scanIpMacHead);
}

int main()
{
    //定义路由器关闭信号
    signal(SIGTERM, myexit);

    // 创建原始套接字
    int sfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if(sfd < 0){
        printf("socket");
        DEBUGERROR;
        exit(0);
    }

    // 处理局域网数据的数据接受和处理线程
    pthread_t ptdFd0;
    pthread_t menuPtd;
    ipMacIfo *ifoIpMacFd = (ipMacIfo*)malloc(sizeof(ipMacIfo));

    ifoIpMacFd->sockFd = sfd;
    ifoIpMacFd->ptdfd = ptdFd0;
    
    arpScanFunc(ifoIpMacFd);  
    //menuHelp(); // 显示菜单

    msleep(10);
    pthread_create(&ptdFd0, NULL, rawSocket, ifoIpMacFd);
    pthread_join(ptdFd0, NULL);
  
    return 0;
}

